/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.put.netbeans.dcs_modeler.actions;

/**
 *
 * @author rudy
 */
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Logger;
import org.openide.cookies.CloseCookie;
import org.openide.cookies.OpenCookie;
import org.openide.loaders.OpenSupport;
import org.openide.windows.CloneableTopComponent;
import org.put.netbeans.dcs_modeler.ComputingObjectDefinitionEditorTopComponent;
import org.put.netbeans.dcs_modeler.dataObjects.DCSComputingObjectDefinitionDataObject;

public class DCSComputingObjectOpenSuport extends OpenSupport implements OpenCookie, CloseCookie
{
    private static Logger log =
            Logger.getLogger( DCSComputingObjectOpenSuport.class.getName() );
    private DCSComputingObjectDefinitionDataObject dataObj;
    private ComputingObjectDefinitionEditorTopComponent topComponent;

    public DCSComputingObjectOpenSuport( DCSComputingObjectDefinitionDataObject.Entry entry )
    {
        super( entry );
    }

    @Override
    protected CloneableTopComponent createCloneableTopComponent()
    {
        dataObj = ( DCSComputingObjectDefinitionDataObject )entry.getDataObject();
        topComponent = new ComputingObjectDefinitionEditorTopComponent();
        topComponent.setDisplayName( dataObj.getName() );
        // ladowanie portow
        loadWorkspace();
        return topComponent;
    }    
    
    private void loadWorkspace()
    {
        Iterator< HashMap< String, Object > > portIterator = dataObj.getPortInVector().iterator();
        while ( portIterator.hasNext() )
        {
            HashMap< String, Object > tmpEl = portIterator.next();
            topComponent.addPortIn( ( String )tmpEl.get( "name" ), ( String )tmpEl.get( "signalType" ) );
        }
        // ladowanie wyjsc
        portIterator = dataObj.getPortOutVector().iterator();
        while ( portIterator.hasNext() )
        {
            HashMap< String, Object > tmpEl = portIterator.next();
            topComponent.addPortOut( ( String )tmpEl.get( "name" ), ( String )tmpEl.get( "signalType" ) );
        }
    }
    
    public void saveComputingObject()
    {
        dataObj.setPortInVector( getPortInList() );
        dataObj.setPortOutVector( getPortOutList() );
    }
    
    private HashMap< String, Object > getParamsFromRow( Vector row )
    {
        HashMap< String, Object > ret = new HashMap();
        ret.put( "name", row.get( 0 ) );
        ret.put( "signalType", row.get( 1 ) );
        return ret;
    }
    
    public Vector< HashMap< String, Object > > getPortInList()
    {
        Vector< HashMap< String, Object > > ret = new Vector();
        Iterator it = topComponent.getPortInVector().iterator();
        while ( it.hasNext() )
        {
            ret.add( getParamsFromRow( (Vector)it.next() ) );
        }
        return ret;
    }
    
    public Vector< HashMap< String, Object > > getPortOutList()
    {
        Vector< HashMap< String, Object > > ret = new Vector();
        Iterator it = topComponent.getPortOutVector().iterator();
        while ( it.hasNext() )
        {
            ret.add( getParamsFromRow( (Vector)it.next() ) );
        }
        return ret;
    }
    
}
